<?php
/**
 * Created by PhpStorm.
 * User:毛文科
 * Date: 2019/5/25
 * Time: 19:28
 */
namespace app\admin\model;
use think\Db;
use think\Exception;
use think\Request;

class Jurisdiction{
    static public function inspect(Request $request){
        $data = $request->param();
        $controller = $request->controller();
        $action = $request->action();
        if($controller!=='Index'&&$controller!=='Api'&&$controller!=='Huisou'){
            if($action=='page'||$action=='show_list'||$action=='detail'){
                $quanxian = '查';
            }elseif ($action=='change'||$action=='edit'||$action=='show_edit'||strpos($action,'change')!==false){
                $quanxian = '改';
            }elseif ($action=='add'||$action=='show_add'){
                $quanxian = '增';
            }elseif ($action=='del'){
                $quanxian = '删';
            }else{
                $quanxian = '查';
            }
            $controller = strtolower($controller);
            if($controller=='document'){
                Jurisdiction::cidJur($data['cid'],$quanxian);
            }elseif ($controller=='lianbiao'){
                Jurisdiction::ModelIdJur($data['model_id'],$quanxian);
            }else{
                if(isset($data['cid'])&&$data['cid']){
                    Jurisdiction::cidJur($data['cid'],$quanxian);
                }else{
                    if($controller=='adminuser'){
                        $controller = 'admin_user';
                    }elseif ($controller=='databassystem'){
                        $controller = 'database_system';
                    }
                    Jurisdiction::ModelJur($controller,$quanxian);
                }
            }
        }
    }
    static public function cidJur($cid,$quan){
        $user = session('adminuser');
        $role = Roles::where('id',$user['role_id'])->json(['role'])->findOrEmpty()->toArray();
        $jur = Jurs::where('cid',$cid)->where('descs',$quan)->findOrEmpty()->toArray();
//        echo $jur['id'];
        if(!in_array($jur['id'],$role['role'])){
            echo '权限不足';exit;
        }

    }
    static public function ModelJur($modelname,$quan){
        $modelname = session('modular').'_'.$modelname;
        $model = Db::name(session('modular').'_models')->where('model_table','like',"$modelname%")->find();
        $user = session('adminuser');
        $role = Roles::where('id',$user['role_id'])->json(['role'])->findOrEmpty()->toArray();
        $jur = Jurs::where('model_id',$model['id'])->where('descs',$quan)->findOrEmpty()->toArray();
//        echo $jur['id'];
        if(!in_array($jur['id'],$role['role'])){
            echo '权限不足';exit;
        }
    }
    static public function ModelIdJur($id,$quan){
        $user = session('adminuser');
        $role = Roles::where('id',$user['role_id'])->json(['role'])->findOrEmpty()->toArray();
        $jur = Jurs::where('model_id',$id)->where('descs',$quan)->findOrEmpty()->toArray();
//        echo $jur['id'];
        if(!in_array($jur['id'],$role['role'])){
            echo '权限不足';exit;
        }
    }
}